/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain one copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sample.prdownloader.util;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.sample.prdownloader.ResourceTable;

import java.lang.ref.WeakReference;

public class ToastsUtils {
    private ToastDialog toastDialog;

    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x000009, "ToastsUtils");

    public void show() {
        if (toastDialog != null) {
            if (toastDialog.isShowing()) {
                toastDialog.cancel();
                toastDialog.show();
            } else {
                toastDialog.show();
            }
        }
    }

    public ToastsUtils() {
        throw new RuntimeException("can’t init");
    }

    private ToastsUtils(Builder builder) {
        if (toastDialog == null) {
            toastDialog = new ToastDialog((Context) builder.mContext.get());
        }
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance((Context) builder.mContext.get())
            .parse(ResourceTable.Layout_test_toast, null, false);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(150, 150, 150));
        background.setAlpha(46);
        background.setCornerRadius(60);
//        background.setStroke(6, new RgbColor(255, 120, 120));
        toastDialog.setTransparent(true);
        toastDialog.setDuration(builder.mDuration);
        toastDialog.setAlignment(builder.mAlignment);
        if (toastLayout == null) {
            return;
        }
        toastLayout.setBackground(background);
        if (builder.mToastText != null) {
            ((Text) toastLayout.findComponentById(ResourceTable.Id_msg1_toast)).setText(builder.mToastText);
        }
        if (builder.mComponent != null) {
            toastDialog.setComponent(builder.mComponent);
        } else {
            toastDialog.setComponent(toastLayout);
        }
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
    }

    public static final class Builder {
        private WeakReference mContext;
        //显示时间
        private int mDuration = 2000;
        private String mToastText = "vv";
        //对齐模式
        private int mAlignment = BOTTOM;

        public static final int BOTTOM = 80;
        public static final int CENTER = 17;
        public static final int END = 8388613;
        public static final int HORIZONTAL_CENTER = 1;
        public static final int LEFT = 3;
        public static final int RIGHT = 5;
        public static final int START = 8388611;
        public static final int TOP = 48;
        public static final int UNSET = 0;
        public static final int VERTICAL_CENTER = 16;
        //自定义UI界面
        private DirectionalLayout mComponent;


        public Builder(Context context) {
            mContext = new WeakReference(context);
        }

        public Builder setDuration(int mDuration) {
            this.mDuration = mDuration;
            return this;
        }

        public Builder setToastText(String mToastText) {
            this.mToastText = mToastText;
            return this;
        }

        public Builder setAlignment(int mAlignment) {
            this.mAlignment = mAlignment;
            return this;
        }

        public Builder setComponent(DirectionalLayout mComponent) {
            this.mComponent = mComponent;
            return this;
        }


        public ToastsUtils build() {
            return new ToastsUtils(this);
        }
    }
}

